package ru.ognick.utils;

import java.io.File;
import ru.ognick.R;
import android.app.Activity;
import android.media.MediaPlayer;
import android.util.Log;

public class Media {
	private final String TAG = "Media";
	private MediaPlayer mediaPlayer = null ;
    public static final String MEDIA_PATH = new String("/sdcard/");
	private Activity own;
	public Media(Activity own){
		this.own = own;
	}
	public void play(String filename){
		Log.d (TAG,"PLAY!!!");
		if (mediaPlayer == null) {
			if (filename == null) 
				mediaPlayer = MediaPlayer.create(own, R.raw.s01);
			else
				try {
					mediaPlayer = new MediaPlayer();
					File file = new File(MEDIA_PATH,filename);
	//				mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
					Log.d(TAG,"FILE: "+filename);
					mediaPlayer.setDataSource(file.getAbsolutePath());
					mediaPlayer.prepare();
				} catch (Exception e) {
					Log.e(TAG,e.getMessage());
				}
		}
		mediaPlayer.setVolume(0.5f, 0.5f);
		mediaPlayer.start();
	}
		
	public void stop(){
		if (mediaPlayer != null && mediaPlayer.isPlaying()) {
			mediaPlayer.stop();
			mediaPlayer.release();
			mediaPlayer = null;
		}
	}
	
}
